< previous page page_81 next page >

Page 81
The Factory Pattern Example: An Overview
Factories provide a single interface for creating a particular object without your specifying their Visual Basic classes. One key benefit of this pattern is that it enables you to control groups of associated classes that your application creates at runtime. A related benefit is that a factory enables you the flexibility of decoupling a particular framework you use from the knowledge of classes the framework can't anticipate. This helps maintain consistency across your applications without requiring much effort.
Note
The runtime architecture of an application is more complex than the designtime architecture. This is because, among other things, many instances of classes interact at runtime.

Learning How to Implement the Factory Pattern in Visual Basic
Typically, you'll need only one instance of the concrete factory per application subsystem. The concrete factory object is responsible for implementing the interface of the abstract factory. Keep in mind that abstract classes don't give birth to (lead to instances of) real-world objects, but to concrete classes. Concrete classes are created as real-world objects. Finally, because Visual Basic doesn't support implementation inheritance, you need a mix of both interface inheritance and delegation to fully use this pattern.
An Easy Example
In keeping with the banking theme of this book, let's say you've discovered that your bank client offers several account products to its customers. Such products include Personal Checking Plus and Personal Savings Plus. Operations such as opening a checking account, verifying check writing history, and so on, still apply and are part of the core of the application. On detailed analysis of the requirements, you discover that the main differences between these two products are as follows:
Personal Savings Plus lets your money earn interest at 3% compounded monthly.
Personal Checking Plus has overdraft protection.
Your client then tells you that if a customer doesn't want either of these products, he can still choose Basic Personal Savings and Basic Personal Checking accounts. Fortunately for you, your client has bankwide codes for each of these products, as detailed in Table 3.1.

 
< previous page page_81 next page >

If you like this book, buy it!